# -*- coding:utf-8 -*-
import time,xlrd,json


class commonpage(object):

    #把float转换为int
    def ChangeFloatToInt(self,num):
        '''
        :return: 返回int(num)
        '''
        if type(num) == float:
            return int(num)
        else:
            return num

    #获得当前日期
    def GetTime(self,t):
        '''
        :param t: '%Y-%m-%d %H:%m'
        :return: 时间格式字符串
        '''
        time_prefix = time.strftime(t, time.localtime())
        return time_prefix

    #读取excel表中数据
    def GetExcel(self, file_path, sheet_name,name,num):
        '''
        :param file_path: 文件路径
        :param sheet_name: sheet表名
        :param name: 名为name的行
        :param num: 第几列
        '''
        book = xlrd.open_workbook(file_path)
        sheet = book.sheet_by_name(sheet_name)
        rows = sheet.nrows
        data = {}
        for i in range(rows):
            t = sheet.row_values(i)  # 读取第i行的数据
            case = {}
            case[name] = self.ChangeFloatToInt(t[num-1])     #读取名为case的行，第num列数据
            CASE = str(i)
            data[CASE] = case
        return data

    # 加载json
    def LoadJson(self, path, name):
        '''
        :param path: 路径
        :param name: json—key
        '''
        with open(path) as file_obj:
            kwargs = json.load(file_obj)
        data = kwargs.get(name)
        return data